package com.whu.aircraft;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.whu.util.JacksonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

import static com.whu.util.JacksonUtil.objectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("track_info")
public class TrackInfo {
    @TableId(type = IdType.AUTO)
    private Long trackId;
    private String trackName;
    private String trackJson;
    @TableField(exist = false)
    private ArrayList<RoutePoint> track;

    public void list2Json() throws JsonProcessingException {
        this.trackJson = JacksonUtil.objectMapper.writeValueAsString(this.track);
    }
    public void json2List() throws JsonProcessingException {
        CollectionType collectionType = objectMapper.getTypeFactory()
                .constructCollectionType(ArrayList.class,RoutePoint.class);
        this.track = objectMapper.readValue(this.trackJson,collectionType);
    }
}
